//
//  XHBankCardPickerViewController.m
//  elxc
//
//  Created by 冷山 on 16/3/30.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHBankCardPickerViewController.h"

#import "Services.h"

@interface XHBankCardPickerViewController ()
@property (weak, nonatomic) IBOutlet UIButton *ui_cancelButton;
@property (weak, nonatomic) IBOutlet UIButton *ui_okButton;
@property (weak, nonatomic) IBOutlet UIPickerView *ui_pickerView;
@property (strong, nonatomic) NSArray * m_bankArray;
@property (strong, nonatomic) NSDictionary *selectedBank;

@end

@implementation XHBankCardPickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!self.m_bankArray)
    {
        [ServiceManager getBankInfoWithSuccess:^(NSDictionary *data) {
            self.m_bankArray= (NSArray*)data;
            [self.ui_pickerView reloadAllComponents];
        } failure:^(NSDictionary *data) {
            
        }];
        
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)cancelButtonAction:(id)sender {
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}
- (IBAction)okButtonAction:(id)sender {

    if ([self.selectedBank isKindOfClass:[NSDictionary class]])
    {
        if (self.selectedBank)
        {
            [self.delegate didSelectedBankDictionary:self.selectedBank];
        }
        else
        {
            [self.delegate didSelectedBankDictionary:self.m_bankArray.firstObject];
        }
    }
    else
    {
        [self.delegate didSelectedBankDictionary:self.m_bankArray.firstObject];
    }
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}

#pragma mark - UIPickerViewDelegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if([self.m_bankArray isKindOfClass:[NSArray class]])
    {
        self.selectedBank=self.m_bankArray[row];
    }
    else
    {
        
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSDictionary *dic=self.m_bankArray[row];
    return dic[@"dictDataValue"];
}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger rows = 0;
    if([self.m_bankArray isKindOfClass:[NSArray class]])
    {
        rows = self.m_bankArray.count;
    }
    else
    {
        rows = 0;
    }
    return rows;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
